#pragma once
#include <functional>
#include <iostream>
#include <string>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <cstring>
#include <unistd.h>
#include <cassert>

const std::string optionlist = "+-*/%";

int mymath(int x , int y , char op){
    int result;
    switch(op){
        case '+':   
            result = x+y;
            break;
        case '-':   
            result = x-y;
            break;
        case '*':   
            result = x*y;
            break;
        case '/':{
            if(y == 0){
                std::cerr<<"zero fild by '/'"<<std::endl;
                result = -1;
            }else{
                result = x/y;
            }
            break;
        }
        case '%': {
            if(y == 0){
                std::cerr<<"zero fild by '%'"<<std::endl;
                result = -1;
            }else{
                result = x%y;
            }
            break;
        }
        default:
            result = -1;
            break;
    }
    return result;
}

class Task{
public:
    using func_t = std::function<int(int,int,char)>;
public:
    Task(){}
    Task(int x ,int y, char op, func_t callback):_x(x),_y(y),_op(op),_callback(callback) {}

    std::string operator() (){
        int result = _callback(_x,_y,_op);
        char buffer[512];
        snprintf(buffer,sizeof buffer,"%d %c %d = %d",_x,_op,_y,result);
        return buffer;
    }
    std::string tostring(){
        char buffer[512];
        snprintf(buffer,sizeof buffer,"%d %c %d = ?",_x,_op,_y);
        return buffer;
    }
private:
    int _x;
    int _y;
    char _op;
    func_t _callback;

};

void save(const std::string& message){
    const char* fileadr = "./log.txt";
    int fd = open(fileadr,O_WRONLY|O_APPEND|O_CREAT,0666);
    ssize_t r = write(fd,(void*)message.c_str(),strlen(message.c_str()) );
    const char t = '\n';
    write(fd,&t,1);
    assert(r != -1);

    close(fd);
    
}


class Save{
public:
    using func_t = std::function<void(std::string)>;
public:
    Save(){};
    Save(const std::string s , func_t savecallback):_save_date(s),_savecallback(savecallback){}

    void operator() (){
        _savecallback(_save_date);
        return;
    }
private:
    std::string _save_date;
    func_t _savecallback;

};